﻿using JXC.Core.Models;
using JXC.EFCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OutOrderController : ControllerBase//出库
    {
        private readonly AppDbContext _context;

        public OutOrderController(AppDbContext context)
        {
            _context = context;
        }
        [HttpGet]
        public IEnumerable<OutboundDeliveryOrder> GetAll()
        {
            return _context.OutboundDeliveryOrder.ToList();
        }

        /// <summary>
        /// 根据Id查找数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<OutboundDeliveryOrder> GetById(Guid id)
        {
            var item = await _context.OutboundDeliveryOrder.FirstOrDefaultAsync(t => t.Id == id);
            return item;
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] OutboundDeliveryOrder nvent)
        {
            if (nvent == null)
            {
                return BadRequest();
            }
            else
            {
                _context.OutboundDeliveryOrder.Add(nvent);
                await _context.SaveChangesAsync();
                return Ok(nvent);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cinfo"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, [FromBody] OutboundDeliveryOrder nvent)
        {
            if (nvent == null)
            {
                return BadRequest();
            }
            else
            {
                var nvents = _context.OutboundDeliveryOrder.SingleOrDefault(t => t.Id == id);
                if (nvents == null)
                {
                    return NotFound();
                }
                else
                {
                    nvents.SalesDate = nvent.SalesDate;
                    nvents.Client = nvent.Client;
                    _context.OutboundDeliveryOrder.Update(nvents);
                    await _context.SaveChangesAsync();
                    return Ok(nvents);
                }
            }
        }
        // DELETE api/<CkAdminController>/5
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var nvent = _context.OutboundDeliveryOrder.SingleOrDefault(t => t.Id == id);
            if (nvent == null)
            {
                return NotFound();
            }
            else
            {
                _context.OutboundDeliveryOrder.Remove(nvent);
                await _context.SaveChangesAsync();
                return Ok();
            }
        }
    }
}
